set linesize 140
set rmsg on
set scheme s1mono
/***************************************************************************
	Project		:	Army Service in the All-Volunteer Era	
	Author(s)	:	Kyle Greenberg	(kyle.greenberg@westpoint.edu)
					Matthew Gudgeon (matthew.gudgeon@westpoint.edu)
					Adam Isen 		(Adam.Isen@treasury.gov)
					Corbin Miller 	(Corbin.Miller@treasury.gov)
					Rich Patterson 	(rich_patterson@byu.edu)
	File Name	:	table_a_10.do
	Description	:	This file runs the analysis for table a.10
****************************************************************************/

*---- if not running master, set up file structure ----*
*change directory to where programs and subfolders are stored
cd ""

if "${raw}"==""		global raw		"raw/"
if "${data}"==""	global data		"data/"
if "${output}"==""	global output	"output/"

cap mkdir	"${output}"

*at10 -- reweighting to overall

use "${data}army-treasury-analysis", clear

keep pid hor quarter wages_m1 anywages_m1 anyf1098T_m1 educ_coll_grad educ_some_coll educ_hsdip educ_ged educ_inhs educ_lessthanhs male age_days access firstyear wages_* firstafqt white black hisp firstafqt_fy  k31  instk31 k31_2 instk31_2 inst31  k50  instk50 k50_2 instk50_2 inst50

merge 1:1 pid using "${raw}army_county_merge.dta", nogen keep(1 3)
drop emp

gen misscounty=0
replace misscount=1 if med==.

replace emp2000=0 if misscount==1
replace singleparent_share1990=0 if misscount==1
replace poor_share1990=0 if misscount==1
replace med_hhinc1990=0 if misscount==1

gen earn_full=0
gen earn_11_19=0
gen count_full=0
gen count_11_19=0

forvalues a = 0/19 {
	replace count_full=count_full+1 if wages_`a'!=.
	if `a'>=11 replace count_11_19=count_11_19+1 if wages_`a'!=.
}

gen wage19=wages_19
forvalues a = 0/19 {
	replace wages_`a'=0 if wages_`a'==.
	replace earn_full=earn_full+wages_`a'
}
forvalues a = 11/19 {
	replace earn_11_19=earn_11_19+wages_`a'
	drop wages_`a'
}

replace earn_full=earn_full/count_full
replace earn_11_19=earn_11_19/count_11_19

keep if missc==0 

local vars  single med poor_share1990 emp2000 age_days emp
foreach a of local vars {
	xtile b`a'=`a', n(5)
}

log using "${output}table_a_10", text replace

expand 2
bys pid: gen count=_n

*reweight to look like all enrollees within AFQT range
gen later=0
replace later=1 if access==1

*---- black ----*
*31 cutoff
quietly logit later   male i.bage_days educ_lessthanhs educ_inhs educ_ged educ_hsdip educ_some_coll educ_coll_grad    i.bemp i.bsingle i.bpoor_share1990 i.bmed  if  inrange(firstafqt,12,49) & black==1 & ((later==1 & count==2) | (count==1 & count_11_19!=0))
predict nx
replace nx=1/(1-nx)*nx
gen nweight_full=nx*count_11_19
gen nweight_11_19=nx*count_11_19

ivregress 2sls earn_11_19 k31  instk31 k31_2 instk31_2 i.quarter  (access=inst31) if inrange(firstafqt,12,49) & black==1 & nweight_full!=. & count==1 [aw=nweight_full], r
drop nx nweight* 

*50 cutoff
quietly logit later   male i.bage_days educ_lessthanhs educ_inhs educ_ged educ_hsdip educ_some_coll educ_coll_grad    i.bemp i.bsingle i.bpoor_share1990 i.bmed  if    inrange(firstafqt,31,68) & black==1  & ((later==1 & count==2) | (count==1 & count_11_19!=0))
predict nx
replace nx=1/(1-nx)*nx
gen nweight_full=nx*count_11_19
gen nweight_11_19=nx*count_11_19

ivregress 2sls earn_11_19 k50  instk50 k50_2 instk50_2 i.quarter  (access=inst50) if inrange(firstafqt,31,68)   & black==1 & nweight_full!=. & count==1 [aw=nweight_full], r 
drop nx nweight* 

*---- white ----*
*31 cutoff
quietly logit later   male i.bage_days educ_lessthanhs educ_inhs educ_ged educ_hsdip educ_some_coll educ_coll_grad    i.bemp i.bsingle i.bpoor_share1990 i.bmed  if  inrange(firstafqt,12,49) & white==1 & ((later==1 & count==2) | (count==1 & count_11_19!=0))
predict nx
replace nx=1/(1-nx)*nx
gen nweight_full=nx*count_11_19
gen nweight_11_19=nx*count_11_19

ivregress 2sls earn_11_19 k31  instk31 k31_2 instk31_2 i.quarter  (access=inst31) if inrange(firstafqt,12,49) & white==1 & nweight_full!=. & count==1 [aw=nweight_full], r
drop nx nweight* 

*50 cutoff
quietly logit later   male i.bage_days educ_lessthanhs educ_inhs educ_ged educ_hsdip educ_some_coll educ_coll_grad    i.bemp i.bsingle i.bpoor_share1990 i.bmed  if    inrange(firstafqt,31,68) & white==1  & ((later==1 & count==2) | (count==1 & count_11_19!=0))
predict nx
replace nx=1/(1-nx)*nx
gen nweight_full=nx*count_11_19
gen nweight_11_19=nx*count_11_19

ivregress 2sls earn_11_19 k50  instk50 k50_2 instk50_2 i.quarter  (access=inst50) if inrange(firstafqt,31,68)   & white==1 & nweight_full!=. & count==1 [aw=nweight_full], r 

log close
